package org.neusoft.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.neusoft.dto.Article;
import org.neusoft.dto.ArticleCondition;
import org.neusoft.dto.ArticleInfo;
import org.neusoft.entity.ArticleTags;
import org.neusoft.entity.Articles;
import org.neusoft.entity.Tags;
import org.neusoft.mapper.ArticleTagMapper;
import org.neusoft.mapper.ArticlesMapper;
import org.neusoft.mapper.TagMapper;
import org.neusoft.service.IArticleService;
import org.neusoft.vo.ArticleInfoVo;
import org.neusoft.vo.ArticleVo;
import org.neusoft.vo.ArticlesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/17 14:41
 * @DESCRIBE
 */
@Service
public class ArticleService implements IArticleService {
    @Autowired
    ArticlesMapper articlesMapper;

    @Autowired
    TagMapper tagMapper;

    @Autowired
    ArticleTagMapper articleTagMapper;

    @Override
    public int createArticle(ArticleInfo article, long userId) {
        Article articleDto = article.getArticle();
        Articles articles = new Articles();

        articles.setTitle(articleDto.getTitle());
        articles.setDescription(articleDto.getDescription());
        articles.setBody(articleDto.getBody());

        articles.setUserId(userId);

        System.out.println(articles.getId());
        articlesMapper.insert(articles);
        System.out.println(articles.getId());

        for ( String tag : articleDto.getTagList() ) {
            QueryWrapper<Tags> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", tag);
            Tags tags = tagMapper.selectOne(queryWrapper);
            ArticleTags articleTags = new ArticleTags();
            articleTags.setArticleId(articles.getId());
            if (tags != null) {
                articleTags.setTagId(tags.getId());
            }
            else {
                Tags tagsTmp = new Tags();
                tagsTmp.setName(tag);
                tagMapper.insert(tagsTmp);

                articleTags.setTagId(tagsTmp.getId());
            }
            articleTagMapper.insert(articleTags);
        }
        return 0;
    }

    @Override
    public ArticlesVo getAllArticles(ArticleCondition articleCondition) {
        List<ArticleVo> allArticles = articlesMapper.getAllArticles(articleCondition);
        for (ArticleVo articleVo : allArticles) {
            if (articleVo.getTagString() != null) {
                String[] arr = articleVo.getTagString().split(",");
                List<String> list = Arrays.asList(arr);
                articleVo.setTagList(list);
            }
        }
        ArticlesVo articles = new ArticlesVo();
        int articlesCount = articlesMapper.getArticleCount(articleCondition);
        articles.setArticlesCount(articlesCount);
        articles.setArticles(allArticles);
        return articles;
    }

    @Override
    public ArticleInfoVo getArticleDetail(long aid) {
        ArticleVo articleDetail = articlesMapper.getArticleDetail(aid);
        if (articleDetail.getTagString() != null) {
            String[] arr = articleDetail.getTagString().split(",");
            List<String> list = Arrays.asList(arr);
            articleDetail.setTagList(list);
        }
        ArticleInfoVo articleInfo = new ArticleInfoVo();
        articleInfo.setArticleInfoVo(articleDetail);
        return articleInfo;
    }


//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void foo() {
//        Tags tagsTmp = new Tags();
//        tagsTmp.setName("加装事务之后如果出错，则会回滚");
//        tagMapper.insert(tagsTmp);
//        int i = 90 / 0;
//        QueryWrapper<Tags> queryWrapper = new QueryWrapper<Tags>();
//        queryWrapper.eq("name","java");
//        tagMapper.delete(queryWrapper);
//    }
}
